# Copyright 2022 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@yacl//bazel:yacl.bzl", "AES_COPT_FLAGS")
load("//bazel:psi.bzl", "psi_cc_library", "psi_cc_test")

package(default_visibility = ["//visibility:public"])

psi_cc_library(
    name = "multiplex_disk_cache",
    srcs = ["multiplex_disk_cache.cc"],
    hdrs = ["multiplex_disk_cache.h"],
    deps = [
        ":io",
        "@boost//:uuid",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_test(
    name = "multiplex_disk_cache_test",
    srcs = ["multiplex_disk_cache_test.cc"],
    deps = [
        ":multiplex_disk_cache",
    ],
)

psi_cc_library(
    name = "hash_bucket_cache",
    srcs = ["hash_bucket_cache.cc"],
    hdrs = ["hash_bucket_cache.h"],
    deps = [
        ":arrow_csv_batch_provider",
        ":multiplex_disk_cache",
        "@com_google_absl//absl/strings",
    ],
)

psi_cc_library(
    name = "csv_checker",
    srcs = ["csv_checker.cc"],
    hdrs = ["csv_checker.h"],
    deps = [
        ":io",
        ":key",
        "@boost//:uuid",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "@org_apache_arrow//:arrow",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/crypto/hash:hash_utils",
        "@yacl//yacl/utils:scope_guard",
    ],
)

psi_cc_library(
    name = "serialize",
    hdrs = ["serialize.h"],
    deps = [
        ":serializable_cc_proto",
        "@yacl//yacl/base:buffer",
    ],
)

proto_library(
    name = "serializable_proto",
    srcs = ["serializable.proto"],
)

cc_proto_library(
    name = "serializable_cc_proto",
    deps = [":serializable_proto"],
)

psi_cc_library(
    name = "csv_header_analyzer",
    hdrs = ["csv_header_analyzer.h"],
    deps = [
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_test(
    name = "csv_checker_test",
    srcs = ["csv_checker_test.cc"],
    deps = [
        ":csv_checker",
    ],
)

psi_cc_library(
    name = "ec_point_store",
    srcs = ["ec_point_store.cc"],
    hdrs = ["ec_point_store.h"],
    linkopts = select({
        "@bazel_tools//src/conditions:darwin": [],
        "//conditions:default": ["-fopenmp"],
    }),
    deps = [
        ":hash_bucket_cache",
        ":index_store",
        "@yacl//yacl/link",
    ],
)

psi_cc_library(
    name = "batch_provider",
    srcs = ["batch_provider.cc"],
    hdrs = ["batch_provider.h"],
    deps = [
        ":csv_header_analyzer",
        ":io",
        ":key",
        "@com_google_absl//absl/strings",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/crypto/rand",
    ],
)

psi_cc_library(
    name = "resource",
    srcs = ["resource.cc"],
    hdrs = ["resource.h"],
    deps = [
        ":hash_bucket_cache",
        "@com_google_absl//absl/strings",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_library(
    name = "test_utils",
    hdrs = [
        "test_utils.h",
    ],
    deps = [
        "//psi/proto:psi_cc_proto",
        "@yacl//yacl/crypto/hash:hash_utils",
    ],
)

psi_cc_library(
    name = "sync",
    srcs = ["sync.cc"],
    hdrs = ["sync.h"],
    deps = [
        ":serialize",
        "@yacl//yacl/link",
    ],
)

psi_cc_library(
    name = "emp_io_adapter",
    srcs = ["emp_io_adapter.cc"],
    hdrs = [
        "emp_io_adapter.h",
    ],
    deps = [
        "@com_github_emptoolkit_emp_tool//:emp-tool",
        "@yacl//yacl/link",
    ],
)

psi_cc_test(
    name = "emp_io_adapter_test",
    srcs = ["emp_io_adapter_test.cc"],
    copts = AES_COPT_FLAGS,
    deps = [
        "emp_io_adapter",
    ],
)

psi_cc_library(
    name = "ub_psi_cache",
    srcs = ["ub_psi_cache.cc"],
    hdrs = ["ub_psi_cache.h"],
    deps = [
        ":batch_provider",
        ":io",
        ":serialize",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_test(
    name = "ub_psi_cache_test",
    srcs = ["ub_psi_cache_test.cc"],
    deps = [
        ":ub_psi_cache",
        "@boost//:uuid",
        "@com_google_absl//absl/time",
        "@yacl//yacl/crypto/rand",
        "@yacl//yacl/utils:scope_guard",
    ],
)

psi_cc_library(
    name = "progress",
    srcs = ["progress.cc"],
    hdrs = ["progress.h"],
    deps = [
        "@com_github_fmtlib_fmt//:fmtlib",
    ],
)

psi_cc_test(
    name = "progress_test",
    srcs = ["progress_test.cc"],
    deps = [
        ":progress",
        "@com_google_googletest//:gtest",
    ],
)

psi_cc_library(
    name = "arrow_csv_batch_provider",
    srcs = ["arrow_csv_batch_provider.cc"],
    hdrs = ["arrow_csv_batch_provider.h"],
    deps = [
        ":batch_provider",
        ":key",
        "@org_apache_arrow//:arrow",
    ],
)

psi_cc_test(
    name = "arrow_csv_batch_provider_test",
    srcs = ["arrow_csv_batch_provider_test.cc"],
    deps = [
        ":arrow_csv_batch_provider",
    ],
)

psi_cc_library(
    name = "advanced_join",
    srcs = ["advanced_join.cc"],
    hdrs = ["advanced_join.h"],
    deps = [
        ":key",
        "//psi:prelude",
        "//psi/proto:psi_v2_cc_proto",
        "@boost//:uuid",
        "@org_apache_arrow//:arrow",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/link",
    ],
)

psi_cc_test(
    name = "advanced_join_test",
    srcs = ["advanced_join_test.cc"],
    deps = [
        ":advanced_join",
    ],
)

psi_cc_library(
    name = "index_store",
    srcs = ["index_store.cc"],
    hdrs = ["index_store.h"],
    deps = [
        "@org_apache_arrow//:arrow",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_test(
    name = "index_store_test",
    srcs = ["index_store_test.cc"],
    deps = [
        ":index_store",
    ],
)

psi_cc_library(
    name = "csv_header_parser",
    srcs = ["csv_header_parser.cc"],
    hdrs = ["csv_header_parser.h"],
    deps = [
        "@org_apache_arrow//:arrow",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_test(
    name = "csv_header_parser_test",
    srcs = ["csv_header_parser_test.cc"],
    deps = [
        ":csv_header_parser",
    ],
)

psi_cc_library(
    name = "recovery",
    srcs = [
        "recovery.cc",
    ],
    hdrs = [
        "recovery.h",
    ],
    deps = [
        ":io",
        "//psi/cryptor:ecc_cryptor",
        "//psi/proto:psi_v2_cc_proto",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/link",
    ],
)

psi_cc_test(
    name = "recovery_test",
    srcs = ["recovery_test.cc"],
    deps = [
        ":recovery",
        "//psi/cryptor:cryptor_selector",
    ],
)

psi_cc_library(
    name = "bucket",
    srcs = ["bucket.cc"],
    hdrs = ["bucket.h"],
    deps = [
        ":hash_bucket_cache",
        ":index_store",
        ":recovery",
        ":sync",
        "//psi:prelude",
        "//psi/proto:psi_v2_cc_proto",
    ],
)

psi_cc_library(
    name = "key",
    srcs = [
        "key.cc",
    ],
    hdrs = [
        "key.h",
    ],
    deps = [
        ":csv_header_parser",
        ":io",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_library(
    name = "io",
    srcs = ["io.cc"],
    hdrs = ["io.h"],
    deps = [
        "@yacl//yacl/base:exception",
        "@yacl//yacl/io/rw",
        "@yacl//yacl/io/stream",
    ],
)

psi_cc_library(
    name = "ec",
    srcs = ["ec.cc"],
    hdrs = ["ec.h"],
    deps = [
        ":io",
        "//psi/cryptor:ecc_cryptor",
    ],
)

cc_proto_library(
    name = "ic_protocol_psi_cc_proto",
    deps = ["@org_interconnection//interconnection/runtime:ecdh_psi"],
)

psi_cc_library(
    name = "communication",
    srcs = ["communication.cc"],
    hdrs = ["communication.h"],
    deps = [
        ":ic_protocol_psi_cc_proto",
        ":serialize",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/link",
    ],
)

psi_cc_library(
    name = "cuckoo_index",
    srcs = ["cuckoo_index.cc"],
    hdrs = ["cuckoo_index.h"],
    linkopts = ["-lm"],
    deps = [
        "@com_google_absl//absl/types:span",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/base:int128",
    ],
)

psi_cc_test(
    name = "cuckoo_index_test",
    srcs = ["cuckoo_index_test.cc"],
    deps = [
        ":cuckoo_index",
        "@yacl//yacl/crypto/rand",
    ],
)
